package net.xdclass.util;

/**
 * @Classname CustmeSingleLinke
 * @Description TODO
 * @Date 2022/11/24 17:51
 * @Created by sunhao
 */
public class CustmeSingleLinke {

  public static void main(String[] args) {
    CustmeLinke custmeLinke = new CustmeLinke();
    custmeLinke.addCourse(new CourseNode(1, "class1"));
    custmeLinke.addCourse(new CourseNode(2, "class2"));
    custmeLinke.addCourse(new CourseNode(3, "class3"));
    custmeLinke.updateCourse(3,new CourseNode(8,"class3"));
    custmeLinke.deleteCourse(1);
    custmeLinke.showNode();
    int length = custmeLinke.getLength();
    System.out.println(length);
    CourseNode lastCourse = custmeLinke.getLastCourse(1);
    System.out.println(lastCourse);
  }
}

class CustmeLinke {
  /**
   * 初始化头部节点
   */
  private CourseNode head = new CourseNode(0, "");
  /**
   * 有效节点个数
   */
  private int length = 0;

  /**
   * 添加元素节点
   */
  public void addCourse(CourseNode courseNode) {
    CourseNode cur = head;
    while (true) {
      //寻找最后一个节点
      if (cur.next == null) {
        break;
      }
      cur = cur.next;
    }
    cur.next = courseNode;
    length++;
  }

  /**
   * 编辑节点
   */
  public void showNode() {
    CourseNode cur = head;
    if (cur.next == null) {
      System.out.println("链表无元素");
      return;
    }
    while (true) {
      if (cur.next == null) {
        System.out.println("链表输出完成!");
        return;
      }
      System.out.println(cur.next);
      cur = cur.next;
    }
  }

  /**
   * 根据Id 删除节点
   */
  public void deleteCourse(int id) {
    CourseNode cur = head;
    boolean flag = false;
    while (cur.next != null) {
      if (cur.next.id == id) {
        flag = true;
        break;
      }
      cur = cur.next;
    }
    if (flag) {
      cur.next = cur.next.next;
      length--;
    } else {
      System.out.printf("没有找到要删除的元素", id);
    }
  }

  /**
   * 根据Id 修改节点
   */
  public void updateCourse(int id, CourseNode courseNode) {
    CourseNode cur = head;
    boolean flag = false;
    while (cur.next != null) {
      if (cur.next.id == id) {
        flag = true;
        break;
      }
      cur = cur.next;
    }
    if (flag) {
      cur.next.id = courseNode.id;
      cur.next.courseName = courseNode.courseName;
    } else {
      System.out.printf("没有找到要修改的元素!",id);
    }
  }

  /**
   * 获取元素个数
   */
  public int getLength(){
    return length;
  }

  /**
   * 获取倒数第K个元素
   */
  public CourseNode getLastCourse(int k){
    CourseNode cur = head;
    if(cur.next == null){
      System.out.println("没有可查找的元素!");
      return null;
    }
    int length = getLength();
    if(length < k){
      System.out.println("index out of range");
      return null;
    }
    for (int i = 0; i < length-k ; i++) {
       cur = cur.next;
    }
    return cur.next;
  }
}

class CourseNode {
  /**
   * 节点Id
   */
  public int id;

  /**
   * 节点名称
   */
  public String courseName;

  /**
   * 下一节点指针
   */
  public CourseNode next;

  public CourseNode(int id, String courseName) {
    this.id = id;
    this.courseName = courseName;
    this.next = null;
  }


  @Override
  public String toString() {
    return "CourseNode{" +
        "id=" + id +
        ", courseName='" + courseName + '\'' +
        ", next=" + next +
        '}';
  }
}